<?php
    session_start();    //启动session
    if(isset($_POST['submit'])){  //点击登录按钮
        if($_POST['username']!="" && $_POST['password']!=""){
            $username = $_POST['username'];
            $password = $_POST['password'];
            //数据库查询判断
            require 'Conn.php';
            $sql = "SELECT count(*) FROM student WHERE StudentID='$username' AND Password='$password'";
            $result = $db->query($sql);
            $count = $result->fetch_row()[0];
            if($count>0){
                setcookie('username',$username,time()+60*60*24*7); //创建cookie,7天后失效
                $_SESSION['username']=$username;  //创建session
                echo "<script>alert('登录成功！');location='Index.php';</script>";
            }else{
                echo "<script>alert('登录失败！');location='Login.php';</script>";
            }
            $result->close();
            $db->close();
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .tb2{
            width: 400px;
            margin: 0 auto;
            border-collapse:collapse;
        }
        th,td{
            padding: 5px;
            border:1px solid #ccc;
        }
        th{
            text-align:right;
        }
        td{
            text-align:left;
        }
    </style>
</head>
<body>
    <?php
        include_once "HeaderNav.html";  //引入头部
    ?>
    <h2>用户登录</h2>
    <form action="" method="post">
        <table class="tb2">
            <tr>
                <th>用户名</th><td><input type="text" name="username" value="<?php if(isset($_COOKIE['username'])) echo $_COOKIE['username'] ?>" /></td>
            </tr>
            <tr>
                <th>密码</th><td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <th></th><td><input type="submit" value="登录" name="submit" /></td>
            </tr>
        </table>
    </form>
    <?php
        include_once "Footer.html";  //引入脚部
    ?>
</body>
</html>